package com.furnitur.modules.admin.service.order.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.furnitur.core.entity.order.Order;
import com.furnitur.core.mapper.order.OrderMapper;
import com.furnitur.modules.admin.dto.order.MerchantOrderQueryDTO;
import com.furnitur.modules.admin.service.order.MerchantOrderService;
import com.furnitur.modules.admin.vo.order.MerchantOrderStatisticsVO;
import com.furnitur.common.service.DataPermissionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.math.BigDecimal;

@Service
@Slf4j
public class MerchantOrderServiceImpl implements MerchantOrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private DataPermissionService dataPermissionService;

    @Override
    public IPage<Order> getOrderList(MerchantOrderQueryDTO queryDTO) {
        // 获取当前商家ID
        Long merchantId = dataPermissionService.getCurrentMerchantId();
        
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("merchant_id", merchantId);
        
        // 添加查询条件
        if (StringUtils.hasText(queryDTO.getOrderNo())) {
            wrapper.like("order_no", queryDTO.getOrderNo());
        }
        
        if (queryDTO.getStatus() != null) {
            wrapper.eq("status", queryDTO.getStatus());
        }
        
        if (StringUtils.hasText(queryDTO.getCustomerName())) {
            wrapper.like("receiver_name", queryDTO.getCustomerName());
        }
        
        if (queryDTO.getStartTime() != null) {
            wrapper.ge("created_at", queryDTO.getStartTime());
        }
        
        if (queryDTO.getEndTime() != null) {
            wrapper.le("created_at", queryDTO.getEndTime());
        }
        
        wrapper.orderByDesc("created_at");
        
        return orderMapper.selectPage(
            new Page<>(queryDTO.getPageNum(), queryDTO.getPageSize()),
            wrapper
        );
    }

    @Override
    public Order getOrderDetail(String orderNo) {
        Long merchantId = dataPermissionService.getCurrentMerchantId();
        
        return orderMapper.selectOne(
            new QueryWrapper<Order>()
                .eq("order_no", orderNo)
                .eq("merchant_id", merchantId)
        );
    }

    @Override
    @Transactional
    public void shipOrder(String orderNo, String trackingNo) {
        Long merchantId = dataPermissionService.getCurrentMerchantId();
        
        Order order = orderMapper.selectOne(
            new QueryWrapper<Order>()
                .eq("order_no", orderNo)
                .eq("merchant_id", merchantId)
        );
        
        if (order == null) {
            throw new RuntimeException("订单不存在");
        }
        
        if (order.getStatus() != 1) {
            throw new RuntimeException("订单状态不正确");
        }
        
        // 更新订单状态为已发货
        order.setStatus(2);
        order.setTrackingNo(trackingNo);
        order.setShippingTime(new Date());
        
        orderMapper.updateById(order);
    }

    @Override
    public MerchantOrderStatisticsVO getOrderStatistics() {
        Long merchantId = dataPermissionService.getCurrentMerchantId();
        
        MerchantOrderStatisticsVO vo = new MerchantOrderStatisticsVO();
        
        // 查询各种状态的订单数量
        QueryWrapper<Order> wrapper = new QueryWrapper<Order>()
            .eq("merchant_id", merchantId);
            
        vo.setTotalOrders(orderMapper.selectCount(wrapper).intValue());
        
        wrapper.clear();
        wrapper.eq("merchant_id", merchantId).eq("status", 1);
        vo.setPendingShipOrders(orderMapper.selectCount(wrapper).intValue());
        
        wrapper.clear();
        wrapper.eq("merchant_id", merchantId).eq("status", 2);
        vo.setShippedOrders(orderMapper.selectCount(wrapper).intValue());
        
        // 查询总金额
        BigDecimal totalAmount = orderMapper.sumOrderAmount(merchantId);
        vo.setTotalAmount(totalAmount != null ? totalAmount : BigDecimal.ZERO);
        
        // 获取每日订单统计
        vo.setDailyStats(orderMapper.getDailyOrderStats(merchantId));
        
        return vo;
    }
} 